# This file is part of CAT-SOOP
# Copyright (c) 2011-2016 Adam Hartz <hartz@mit.edu>

# This program is free software: you can redistribute it and/or modify it under
# the terms of the Soopycat License, version 1.

# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the Soopycat License for more details.

# You should have received a copy of the Soopycat License along with this
# program.  If not, see <https://smatz.net/soopycat>.

import re

from http import HTTPStatus


def handle(context):
    uname = context.get('cs_username', 'None')
    logname = 'staticaccess'
    fname = context['filename']

    user = context['cs_user_info']
    perms = user.get('permissions', [])

    rel = context['csm_tutor'].get_release_date(context)
    ts = context['cs_timestamp']
    current = context['csm_time'].from_detailed_timestamp(ts)

    log_entry = {
        k: v
        for (k, v) in context.items()
        if k in {'cs_timestamp', 'cs_path_info', 'cs_ip', 'cs_user_info'}
    }

    m = None
    status = HTTPStatus.OK

    if 'view' not in perms and 'view_all' not in perms or uname == 'None':
        m = 'You are not authorized to view this handout.'
        status = HTTPStatus.UNAUTHORIZED
    elif 'view' in perms and current < rel:
        reltime = context['csm_time'].short_timestamp(rel)
        m = ('This handout is not yet available.  '
             'It will become available at: %s') % reltime
        status = HTTPStatus.NOT_FOUND

    log_entry['success'] = m is None
    context['csm_cslog'].update_log(context['cs_course'], uname, logname,
                                    log_entry)

    if m is None:
        return context['csm_dispatch'].serve_static_file(context, fname, context['cs_env'])
    else:
        return ((status.value, status.phrase), {'Content-type': 'text/plain',
                                'Content-length': str(len(m))}, m)
